package com.au.bescene.activity;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;

import com.au.bescene.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * Shows map with venue.
 * @author Khaled
 *
 */
public class Map extends MapActivity 
{    
	private MapController _mapController;
	private GeoPoint _geoPoint;
	private MapView _mapView;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        
        initializeMap();	
    }
 
    /**
     * Initializes the Map. Sets location. Sets zooming level. Sets pointer icon.
     */
    private void initializeMap() {
		
        _mapView = (MapView) findViewById(R.id.mapview);
        _mapView.displayZoomControls(true);
        _mapView.setBuiltInZoomControls(true);
        
        _mapController = _mapView.getController();
        
        setLocation();
        
        _mapController.animateTo(_geoPoint);
        _mapController.setZoom(21);
        
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = _mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        
 
        _mapView.invalidate();
	}

    /**
     * Sets location of the map depending on chosen venue.
     */
	private void setLocation() {
    	Bundle extras = getIntent().getExtras();
		String venueName = extras.getString("venuename");
		
        double lat = 0.0;
        double longi = 0.0;
        
        if(venueName.equals(getResources().getString(R.string.venuelist_bullys))) {
            lat = -19.2572644;
            longi = 146.8214111;
        	   
        }
        else if(venueName.equals(getResources().getString(R.string.venuelist_flynns))) {
            lat = -19.256853;
            longi = 146.821461;
        }
        else if(venueName.equals(getResources().getString(R.string.venuelist_madcow))){
            lat = -19.257111;
            longi = 146.820871;
        }
        else if(venueName.equals(getResources().getString(R.string.venuelist_monsoons))){
            lat = -19.2578437;
            longi = 146.819525;
        }
        else if(venueName.equals(getResources().getString(R.string.venuelist_riverview))){
            lat = -19.3197745;
            longi = 146.7470301;
        }
        else if(venueName.equals(getResources().getString(R.string.venuelist_thebank))){
            lat = -19.2613631;
            longi = 146.8156114;
        }
        else if(venueName.equals(getResources().getString(R.string.venuelist_thebrewery))){
            lat = -19.25861;
            longi = 146.818312;
        }
        else if(venueName.equals(getResources().getString(R.string.venuelist_theconsortium))){
            lat = -19.257146;
            longi = 146.820075;
        }
        else if(venueName.equals(getResources().getString(R.string.venuelist_thewatermark))){
            lat = -19.248414;
            longi = 146.81203;
        }   
         
        _geoPoint = new GeoPoint((int) (lat *1E6), (int) (longi *1E6));
		
	}

	@Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    
	/**
	 * Represents the marker of the venue on the map.
	 * @author Khaled
	 *
	 */
    class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   
 
            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(_geoPoint, screenPts);
 
            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.maps_arrow);            
            canvas.drawBitmap(bmp, screenPts.x - 8.5f, screenPts.y - 29, null);         
            return true;
        }
    }
}